<template>
  <el-card>
    <el-form :disabled="isShow === 0 ? false : true" class="form-box" :inline="true">
      <el-form-item label="一级分类">
        <el-select
          v-model="primCateId"
          placeholder="请选择一级分类名称"
          style="width: 240px"
          @change="primCateChange"
        >
          <!-- label是下拉框展示的内容；value是下拉框收集的内容 -->
          <el-option
            v-for="c1 in primCateArr"
            :key="c1.id"
            :label="c1.name"
            :value="c1.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select
          v-model="secCateId"
          placeholder="请选择二级分类名称"
          style="width: 240px"
          @change="secCateChange"
          :disabled="!primCateId"
        >
          <el-option
            v-for="c2 in secCateArr"
            :key="c2.id"
            :label="c2.name"
            :value="c2.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select
          v-model="thirdCateId"
          placeholder="请选择三级分类名称"
          style="width: 240px"
          :disabled="!secCateId"
        >
          <el-option
            v-for="c3 in thirdCateArr"
            :key="c3.id"
            :label="c3.name"
            :value="c3.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script setup lang="ts">
import { useCategoryStore } from "@/store/modules/category";
import { storeToRefs } from "pinia";
import { onMounted } from "vue";

// 分类仓库对象
const categoryStore = useCategoryStore();
let {
  primCateId,
  primCateArr,
  secCateArr,
  secCateId,
  thirdCateArr,
  thirdCateId,
} = storeToRefs(categoryStore);
// 控制form表单是否禁用
defineProps(["isShow"]);

onMounted(() => {
  getPrimCategory();
});
// 获取一级分类的数据
const getPrimCategory = () => {
  categoryStore.getPrimCategory();
};
// 一级分类下拉菜单的change事件（选中值改变时触发）
const primCateChange = () => {
  // 清除二级分类和三级分类的数据
  secCateId.value = "";
  thirdCateArr.value = [];
  thirdCateId.value = "";
  // 获取二级分类数据
  categoryStore.getSecCategory();
};
// 二级分类下拉菜单的change事件
const secCateChange = () => {
  // 清除三级分类的数据
  thirdCateId.value = "";
  // 获取三级分类数据
  categoryStore.getThirdCategory();
};
</script>

<style scoped lang="scss">
.form-box {
  display: flex;
  justify-content: space-around;
  align-items: center;
  .el-form-item {
    margin-bottom: 0px;
    margin-right: 0px;
    :deep(.el-form-item__label) {
      padding: 0 18px 0 0;
    }
  }
}
</style>
